const express = require("express");
global.app = express();
const mongoose = require("mongoose");

const log4js= require('./logs/log-config');

const bodyParser = require('body-parser');
const multer = require('multer');
const cors = require('cors');



global.crypto = require("crypto");

global.fs = require('fs');
global.path = require('path');

app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({ limit: '50mb',extended: true }));

app.use(cors());


//生成token
//global.token = require("./jwt/jwt.js");




//日志管理
global.logger = log4js.getLogger();//根据需要获取logger
global.errlogger = log4js.getLogger('err');
global.othlogger = log4js.getLogger('oth');

//管理数据库
global.dbHelper = require( './db/dbHelper' );

const connectDB = require("./db/connectDB");

connectDB.connectDB(mongoose);

//上传文件

var storage = multer.diskStorage({
      //设置上传后文件路径，uploads文件夹会自动创建。
      destination: function (req, file, cb) {
          cb(null, path.resolve(__dirname) +'/public/upload')
      },
      //给上传文件重命名，获取添加后缀名
      filename: function (req, file, cb) {
          cb(null,  Date.now() + file.originalname);
       }
 }); 
   
   
  var upload = multer({
      storage: storage
  });



var uploadFile = require("./api/uploadFile");


app.post("/v1/uploadFile", upload.single("file"),uploadFile.uploadFile);



/* 
  验证码
*/
const getSvgCaptcha = require("./api/getSvgCaptcha");

app.get("/v1/getSvgCaptcha",getSvgCaptcha.getSvgCaptcha);

/*
  token
*/
// const getToken = require("./api/getToken");

// app.get("/v1/getToken", getToken.getToken);

/*
   注册

 */
const register = require("./api/register");

app.post("/v1/articles/register", register.register);

/*
   登录

 */
const login = require("./api/login");

app.post("/v1/articles/login", login.login);

/*
   获取首页文章所有列表：get
 */
const articlesIndex = require('./api/articlesIndex');

app.get("/v1/articles/articlesIndex",articlesIndex.articlesIndex);




/*
   获取文章所有列表：get
   上传文章:post
 */
const articles = require('./api/articles');

app.post("/v1/articles",articles.articles);

app.get("/v1/articles",articles.articles);





/*
 *
 * 获取详细的文章:get
 *
 */


const article = require('./api/article');

app.get("/v1/articles/articlesContent",article.article);


/**
 *更新点赞数量:put
 *
 *  
 */
const watchNum = require("./api/watchNum");

app.put("/v1/articles/:id/:watchNum",watchNum.watchNum);


/*

 获取类型 的文章  get

 */
const articlesType = require("./api/articlesType");

app.get("/v1/articles/articlesType",articlesType.articlesType)


/*
   评论  

 */
const comment = require("./api/comment");

app.post("/v1/articles/comment", comment.comment);

app.get("/v1/articles/initComment",comment.comment)



/*
   首页推荐（根据浏览数）
   method:get

 */
const recommend = require("./api/recommend");

app.get("/v1/articles/recommend", recommend.recommend)


/*
  热门(根据评论数量)
  method:get
 */

const hot = require("./api/hot");

app.get("/v1/articles/hot", hot.hot)

/*

  回復評論

 */

 const answerComment = require("./api/answerComment");

 app.post("/v1/articles/answerComment",answerComment.answerComment)

 app.listen(8000);